# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2022/4/14 11:01 PM
# @Author : 曹庭
# @File : panshare.py
# @Software: PyCharm

from . import api
from course.utils.commons import login_required
from flask import g, current_app, jsonify, request
from course.utils.response_code import RET
from course.models import PanShare, User
from course import db, constants
from course.utils.send_mail import sendmail


@api.route("/add_panshare", methods=["POST"])
# @login_required
def add_panshare():
    """
    添加网盘分享链接
    参数：课程编号，分享链接，分享码
    :return:
    """
    # 获取图片
    request_dict = request.get_json()
    course_id = request_dict.get("course_id")
    share_url = request_dict.get("share_url")
    share_code = request_dict.get("share_code")

    # 校验参数
    if not all([course_id, share_url, share_code]):
        return jsonify(code=RET.PARAMERR, msg="参数不完整")

    # 判断链接是否存在
    try:
        panshare = PanShare.query.filter_by(course_id=course_id).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(code=RET.DBERR, msg="数据库异常")

    if panshare:
        try:
            PanShare.query.filter_by(course_id=course_id).update({"share_url": share_url, "share_code": share_code})
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(e)
            return jsonify(code=RET.DBERR, msg="数据库异常")

        # 返回结果
        return jsonify(code=RET.OK, msg="链接修改成功")


    # 保存用户的注册信息到数据库中
    panshare = PanShare(course_id=course_id, share_url=share_url, share_code=share_code)

    try:
        db.session.add(panshare)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(code=RET.DBERR, msg="数据库异常")

    # 返回结果
    return jsonify(code=RET.OK, msg="链接添加成功")


@api.route("/edit_panshare", methods=["POST"])
@login_required
def edit_panshare():
    """
    修改网盘分享链接
    参数：课程编号，分享链接，分享码
    :return:
    """
    # 获取图片
    request_dict = request.get_json()
    course_id = request_dict.get("course_id")
    share_url = request_dict.get("share_url")
    share_code = request_dict.get("share_code")

    # 校验参数
    if not all([course_id, share_url, share_code]):
        return jsonify(code=RET.PARAMERR, msg="参数不完整")

    try:
        PanShare.query.filter_by(course_id=course_id).update({"share_url": share_url, "share_code": share_code})
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(code=RET.DBERR, msg="数据库异常")

    # 返回结果
    return jsonify(code=RET.OK, msg="链接修改成功")


@api.route("/send_panshare", methods=["POST"])
@login_required
def send_panshare():
    """
    发送网盘分享链接
    参数：课程编号，分享链接，分享码
    :return:
    """
    user_id = g.user_id
    # 获取图片
    request_dict = request.get_json()
    course_id = request_dict.get("course_id")

    # 校验参数
    if not all([course_id]):
        return jsonify(code=RET.PARAMERR, msg="参数不完整")

    try:
        panshare = PanShare.query.filter_by(course_id=course_id).first()
        user = User.query.filter_by(id=user_id).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(code=RET.DBERR, msg="数据库异常")

    sharetext = panshare.to_share()
    mail = user.mail

    sendmail(sharetext, mail)
    # 返回结果
    return jsonify(code=RET.OK, msg="发送成功")


@api.route("/get_panshare_detail", methods=["POST"])
# @login_required
def get_panshare_detail():
    """
    发送网盘分享链接
    参数：课程编号，分享链接，分享码
    :return:
    """
    # user_id = g.user_id
    # 获取图片
    request_dict = request.get_json()
    course_id = request_dict.get("course_id")

    # 校验参数
    if not all([course_id]):
        return jsonify(code=RET.PARAMERR, msg="参数不完整")

    try:
        panshare = PanShare.query.filter_by(course_id=course_id).first()
        if panshare is None:
            return jsonify(code=RET.NOTSET, msg="网盘链接未设置")
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(code=RET.DBERR, msg="数据库异常")

    panshare_dict = panshare.to_dict()

    # 返回结果
    return jsonify(code=RET.OK, msg="网盘链接已设置", data={"panshare_dict": panshare_dict})
